package com.example.elmmerchants;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.example.elmmerchants.First_Page_Fragment.First_Page;
import com.example.elmmerchants.Order_Page.Order_Data;
import com.example.elmmerchants.Ringtone_Setting.Get_Order_Ringtone;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Check_Order_Service extends Service {

    boolean is_loading_data = false, is_order_data = false;
    String email = "";

    public Check_Order_Service() {
    }

    private class Check_Order_Thread extends Thread {
        @Override
        public void run() {
            is_loading_data = false;
            is_order_data = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM `" + email + "_merchant_order` WHERE order_state = ?";
                    PreparedStatement set_data = connection.prepareStatement(sql);
                    set_data.setInt(1, 0);
                    ResultSet set = set_data.executeQuery();
                    while (set.next()) {
                        Order_Data orderData = new Order_Data();
                        orderData.setOrder_state(set.getInt("id"));
                        String sql2 = "update `elm`.`" + email + "_merchant_order` set order_state = ? WHERE id = ?";
                        PreparedStatement preparedStatement = connection.prepareStatement(sql2);
                        preparedStatement.setInt(1, 1);
                        preparedStatement.setInt(2, set.getInt("id"));
                        preparedStatement.executeUpdate();
                        is_order_data = true;
                    }
                    is_loading_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_loading_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (is_order_data) {
                MediaPlayer mediaPlayer = MediaPlayer.create(Check_Order_Service.this, R.raw.new_order);
                mediaPlayer.start();
            }
        }
    }

    private class Re_Loading_Order_Data_Thread extends Thread {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(5000);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Check_Order_Thread checkOrderThread = new Check_Order_Thread();
                checkOrderThread.start();
            }
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @SuppressLint("ForegroundServiceType")
    @Override
    public void onCreate() {
        super.onCreate();
        NotificationManager notification = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel("channelId", "channelId", NotificationManager.IMPORTANCE_HIGH);
            notification.createNotificationChannel(notificationChannel);
        }
        startForeground(1, getNotification());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Re_Loading_Order_Data_Thread reLoadingOrderDataThread = new Re_Loading_Order_Data_Thread();
        reLoadingOrderDataThread.start();
        email = intent.getStringExtra("email");

        return START_NOT_STICKY;
    }

    private Notification getNotification() {
        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle("")
                .setContentText("");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId("channelId");
        }
        return builder.build();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}